package com.joe.blockview.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.joe.blockview.model.Connection;
import com.joe.blockview.model.Oprate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import androidx.annotation.Nullable;

//命令生成服务,命令解析器
public class GenelateCommandService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    public class MyBinder extends Binder {
        public List<List<Oprate>> paraserCon(List<Connection> connectionList) {
            return paraserOprate(connectionList);
        }
    }

    private List<List<Oprate>> paraserOprate(List<Connection> connectionList) {
        //有序排列,有头部的关联处理
        List<List<Oprate>> oprates1 = new ArrayList<>();
        List<Connection> startCons = new ArrayList<>();
        for (Connection connection : connectionList) {
            if (connection.getType() == 0) {
                startCons.add(connection);
            }
        }

        for (Connection connection : startCons) {
            //如果发现开始命令，则根据开始，拼接一条连接
            List<Oprate> cons = new ArrayList<>();
            getConnectLine(connection, cons, connectionList);
            oprates1.add(cons);

            connectionList.remove(connection);
        }


        //Collections.sort(connectionList);
        List<Connection> coloneCons = new ArrayList<>();
        for (Connection connection : connectionList) {
            if (connection.getPreConnection() == null && connection.getType() != 4) {
                coloneCons.add(connection);
            }
        }

        for (Connection connection : coloneCons) {
            //拼接一条连接
            List<Oprate> cons2 = new ArrayList<>();
            getConnectLine(connection, cons2, connectionList);
            oprates1.add(cons2);

            connectionList.remove(connection);
        }
        return oprates1;
    }


    private List<Oprate> getConnectLine(Connection connection, List<Oprate> ops, List<Connection> connectionList) {
        Connection con1 = connection.getTagConnection();
        Connection con2 = connection.getNextConnection();
        Connection con3 = connection.getInputConnection();
        Oprate op = new Oprate();
        op.setCommand(connection.getTagBlockView().getCommand());
        op.setName(connection.getTagBlockView().getName());
        op.setParams(connection.getTagBlockView().getParams());
        op.setWorkspacePoint(connection.getWorkspacePoint());
        op.setType(connection.getType());
        ops.add(op);

        if (con3 != null) {
            Oprate oprate = new Oprate();
            oprate.setCommand(con3.getTagBlockView().getCommand());
            oprate.setName(con3.getTagBlockView().getName());
            oprate.setParams(con3.getTagBlockView().getParams());
            oprate.setWorkspacePoint(con3.getWorkspacePoint());
            oprate.setType(con3.getType());
            op.setInPutOprate(oprate);
            connectionList.remove(con3);
        }
        if (con2 != null) {
            List<Oprate> oprates = new ArrayList<>();
            getConnectLine(con2, oprates, connectionList);
            op.setInOprate(oprates);
            connectionList.remove(con2);
        }
        if (con1 != null) {
            getConnectLine(con1, ops, connectionList);
            connectionList.remove(con1);
        }

        return ops;
    }

}
